package com.zwxbc.funplay.servlet;

import com.google.gson.Gson;
import com.zwxbc.funplay.entity.Developer;
import com.zwxbc.funplay.service.DeveloperService;
import com.zwxbc.funplay.service.impl.DeveloperServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/developer")
public class DeveloperServlet extends HttpServlet {
	DeveloperService developerService = new DeveloperServiceImpl();

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
		request.setCharacterEncoding("UTF-8");
		String action = request.getParameter("action");

		if ("getAll".equals(action)) {
			// 处理获取所有开发商数据
			handleGetList(request, response);
		} else if ("getById".equals(action)) {
			// 处理获取单个开发商数据
			handleGetById(request, response);
		} else {
			// 未知的操作类型
			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
			response.getWriter().println("Unknown action");
		}
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String action = request.getParameter("action");

		if ("add".equals(action)) {
			// 处理添加开发商
			handleAdd(request, response);
		} else if ("update".equals(action)) {
			// 处理更新开发商
			handleUpdate(request, response);
		} else if ("delete".equals(action)) {
			// 处理删除开发商
			handleDelete(request, response);
		} else {
			// 未知的操作类型
			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
			response.getWriter().println("Unknown action");
		}
	}

	// 添加新游戏开发商
	private void handleAdd(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 从请求中提取参数
		String developerName = request.getParameter("developerName");
		String developerUserName = request.getParameter("developerUsername");
		String developerEmail = request.getParameter("developerEmail");
		String developerContact = request.getParameter("developerContact");
		String developerAddress = request.getParameter("developerAddress");

		// 创建一个 Developer 对象
		Developer newDeveloper = new Developer();
		newDeveloper.setDeveloperName(developerName);
		newDeveloper.setDeveloperContact(developerContact);
		newDeveloper.setDeveloperUsername(developerUserName);
		newDeveloper.setDeveloperEmail(developerEmail);
		newDeveloper.setDeveloperAddress(developerAddress);

		// 调用服务方法添加新游戏开发商
		boolean success = developerService.addDeveloper(newDeveloper);

		// 发送响应
		sendJsonResponse(response, "{\"success\":" + success + "}");
	}

	// 更新开发商
	private void handleUpdate(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 从请求中提取参数
		String developerIdParam = request.getParameter("developerId");
		Long developerId = Long.parseLong(developerIdParam);
		String developerName = request.getParameter("developerName");
		String developerUserName = request.getParameter("developerUsername");
		String developerEmail = request.getParameter("developerEmail");
		String developerContact = request.getParameter("developerContact");
		String developerAddress = request.getParameter("developerAddress");

		Developer updatedDeveloper = new Developer();
		updatedDeveloper.setDeveloperId(developerId);
		updatedDeveloper.setDeveloperName(developerName);
		updatedDeveloper.setDeveloperUsername(developerUserName);
		updatedDeveloper.setDeveloperEmail(developerEmail);
		updatedDeveloper.setDeveloperContact(developerContact);
		updatedDeveloper.setDeveloperAddress(developerAddress);

		System.out.println(updatedDeveloper);
		// 调用服务方法更新开发商
		boolean success = developerService.updateDeveloper(updatedDeveloper);
		// 发送响应
		sendJsonResponse(response, "{\"success\":" + success + "}");
	}

	// 删除开发商
	private void handleDelete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 从请求中提取参数
		String developerId = request.getParameter("developerId");

		// 调用服务方法删除开发商
		boolean success = developerService.deleteDeveloper(Long.parseLong(developerId));

		// 发送响应
		sendJsonResponse(response, "{\"success\":" + success + "}");
	}

	// 根据ID查询开发商
	private void handleGetById(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 从请求中提取参数
		String developerIdParam = request.getParameter("developerId");

		Long developerId = Long.parseLong(developerIdParam);

		// 调用方法根据ID获取开发商
		Developer developer = developerService.getDeveloperById(developerId);

		// 将 Developer 对象转换为 JSON 并发送响应
		String jsonData = new Gson().toJson(developer);
		sendJsonResponse(response, jsonData);
	}


	private void handleGetList(HttpServletRequest request, HttpServletResponse response) throws IOException {
		List<Developer> developerList = developerService.getDeveloperList();
		// 将 Developer 对象转换为 JSON 并发送响应
		String jsonData = new Gson().toJson(developerList);
		sendJsonResponse(response, jsonData);
	}

	private void sendJsonResponse(HttpServletResponse response, String jsonData) throws IOException {
		response.setContentType("application/json");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(jsonData);
		out.flush();
	}
}

